package com.neusoft.elmboot.service.impl;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.elmboot.mapper.UserMapper;
import com.neusoft.elmboot.po.User;
import com.neusoft.elmboot.service.UserService;
import com.neusoft.elmboot.util.AESUtils;
import com.neusoft.elmboot.util.TokenUtils;

@Service
public class UserServiceImpl implements UserService{
	String key  = "woniucsdnvip8888";
	
	@Autowired
	private UserMapper userMapper;
	
	
	@Override
	public User getUserByIdByPass(User user) {
		User u=userMapper.getUserByIdByPass(user);
		if(u!=null) {
			String token = TokenUtils.getToken(u.getUserId().toString(), u.getPassword(),"user");
			token=AESUtils.encrypt(token, key);
			u.setToken(token);
			u.setPrivateKey("");
		}
		//System.out.println(token);
		return u;
	}
	
	@Override
	public int getUserById(String userId) {
		return userMapper.getUserById(userId);
	}
	
	@Override
	public int saveUser(User user) {
		return userMapper.saveUser(user);
	}
	
	
	@Override
	public int saveKey(User user){
		int flag=userMapper.saveKey(user);
		return flag;
	}
	
	@Override
	public String getPrivateKey(String userId) {
		return userMapper.getPrivateKey(userId);
	}
	
	@Override
	public String getPublicKey(String userId) {
		return userMapper.getPublicKey(userId);
	}
	
	@Override
	public User getEntireUserById(String userId) {
		User u=userMapper.getEntireUserById(userId);
		if(u!=null) {
			u.setPrivateKey("");
		}
		//System.out.println(token);
		return u;
	}
	
	@Override
	public int updateUser(User user) {
		return userMapper.updateUser(user);
	}
	
	@Override
	public int updateToken(User user) {
		return userMapper.updateToken(user);
	}
	
	
	
	
	
}
	
